{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ba0a7900",
   "metadata": {},
   "source": [
    "---------\n",
    "* Author: Xiao yang\n",
    "\n",
    "* 时间：1125-week13\n",
    "\n",
    "* 资源:\n",
    "> 1. [python官方文档](https://www.python.org/doc/) \n",
    "> 2. [智超博客](https://www.cnblogs.com/xuzhichao)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5551340",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "##  json(文本数据) ————>>>  字典(python中的字典数据)\n",
    "## 字典和列表的综合实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72939f5a",
   "metadata": {},
   "source": [
    "# 本周内容：\n",
    "## 2.1 [集合](https://docs.python.org/3/tutoria/datastructures.html#sets)\n",
    "\n",
    "> 1. 集合的性质\n",
    ">> 1. 无序(unordered)\n",
    ">> 2. 不重复(no duplicate)\n",
    "> 2. 集合的一般方法\n",
    ">> 1. 增加\n",
    ">> 2. 删除\n",
    ">> 3. 查询：in 查询成员关系\n",
    "> 3. 集合的数学逻辑运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fd96b77",
   "metadata": {},
   "source": [
    "### 1. 集合的性质及概念:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7106a1d3",
   "metadata": {},
   "source": [
    "* 思考： 我想了解一组数据中有哪些唯一存在的值？（假如是列表）\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f9fca8b",
   "metadata": {},
   "source": [
    "\n",
    "* 答：以下两种方法均可用于从Python列表中获取唯一值：\n",
    "***\n",
    "* Python set() method[方法]\n",
    "> 1. Python set（）方法\n",
    "* Using Python list.append() method along with a for loop\n",
    "> 2. 使用Python list.append（）方法和for循环\n",
    "* Using Python numpy.unique() method\n",
    "> 3. 使用Python numpy.unique（）方法\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c8f07be",
   "metadata": {},
   "outputs": [],
   "source": [
    "no_duplicate_age = []\n",
    "list_age = [18,24,25,24,18,23,13]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f2a5938",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 24, 25, 23, 13]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "no_duplicate_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c49f8072",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{13, 18, 23, 24, 25}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# list ----> set 的方法：set（）\n",
    "no_duplicate_age_set = set(list_age)\n",
    "no_duplicate_age_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "36adf8a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pear', 'banana', 'orange', 'apple'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方文档测试代码\n",
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket)\n",
    "# 2. in 查询成员关系\n",
    "print('orange' in basket)\n",
    "print('crabgrass' in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41d65ab2",
   "metadata": {},
   "source": [
    "# 集合的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f60fbe5",
   "metadata": {},
   "source": [
    "* 集合的创建的两种方式：\n",
    "> 1. set(list) \n",
    "> 2. 直接按照集合的结构创建：{value1,value2,value3,...}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74214fb4",
   "metadata": {},
   "source": [
    "> 1. 增加add()\n",
    ">> 1. 结果：Add an element to a set\n",
    ">> 2. 注意：This has no effect if the element is already present."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "9d4a3cca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "7d88fe87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function add:\n",
      "\n",
      "add(...) method of builtins.set instance\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "63eb1dbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 4, 7, 8, 55, 66}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1.add(55)\n",
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "7e743c32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 4, 7, 8, 55, 66, 7578}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1.add(7578)\n",
    "set_1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5803ec1",
   "metadata": {},
   "source": [
    "> 2. 删除\n",
    ">> 1. pop()  :   Remove and return an arbitrary set element.Raises KeyError if the set is empty.\n",
    ">> 2. remove()\n",
    ">> 3. discard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "94ccdeff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,3,4,0,8,66,7])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "436ce5c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.set instance\n",
      "    Remove and return an arbitrary set element.\n",
      "    Raises KeyError if the set is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f92c7887",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8}\n",
      "{1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "#  Remove and return an arbitrary set element.\n",
    "print(set_1)\n",
    "set_1.pop()\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "08abb7e4",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'pop from an empty set'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-5f10eeb1835f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 2.  Raises KeyError if the set is empty.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mset_2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mset_2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'pop from an empty set'"
     ]
    }
   ],
   "source": [
    "# 2.  Raises KeyError if the set is empty.\n",
    "set_2 = set([])\n",
    "set_2.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3fb08f9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d7e9cb49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 66, 3, 4, 7, 8}\n",
      "{1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.Remove an element from a set; it must be a member.\n",
    "print(set_1)\n",
    "set_1.remove(66)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9163cf4d",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "66",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-85fbdeb7befa>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 2. If the element is not a member, raise a KeyError. 一般项目中 remove（）会和 in 共同使用\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mset_1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m66\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 66"
     ]
    }
   ],
   "source": [
    "# 2. If the element is not a member, raise a KeyError. 一般项目中 remove（）会和 in 共同使用\n",
    "set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9d7d21c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# * 避免KeyError的方式：\n",
    "if 66 in set_1:\n",
    "    set_1.remove(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "b91e8720",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function discard:\n",
      "\n",
      "discard(...) method of builtins.set instance\n",
      "    Remove an element from a set if it is a member.\n",
      "    \n",
      "    If the element is not a member, do nothing.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.discard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bf753922",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(66)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "9e27d33d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf71f638",
   "metadata": {},
   "source": [
    "> 3. 集合的运算\n",
    ">> 1. 集合的交集\n",
    ">> 2. 集合的并集\n",
    ">> 3. 集合的差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6a45985a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n"
     ]
    }
   ],
   "source": [
    "set_1 = set([1,2,5,6,7,2,3,2,4,4])\n",
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccf4b205",
   "metadata": {},
   "source": [
    ">> 1. 集合的交集\n",
    ">>> 1. 使用集合的方法：set_1.intersection(set_2)\n",
    ">>> 2. 使用逻辑运算符：set_1 & set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "7ccd5c97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function intersection:\n",
      "\n",
      "intersection(...) method of builtins.set instance\n",
      "    Return the intersection of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in both sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.intersection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "988856d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用集合的方法：set_1.intersection(set_2)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1.intersection(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8de5cfae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {1, 3, 4, 7}\n"
     ]
    }
   ],
   "source": [
    "# 使用逻辑运算符：set_1 & set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 & set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "573c4054",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：\n",
    "set_1_xu = {\"星期一12-14\",\"星期二3-5\",\"星期二12-14\" }\n",
    "set_2_前端 = {\"星期二3-5\",\"星期二12-14\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e83e72a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'星期二3-5', '星期二12-14'}\n",
      "结论：周一班 API课 让给前端的同学~\n"
     ]
    }
   ],
   "source": [
    "print(set_1_xu & set_2_前端)\n",
    "print(\"结论：周一班 API课 让给前端的同学~\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01ab3ecb",
   "metadata": {},
   "source": [
    ">> 2. 集合的并集\n",
    ">>> 1. 使用并集的方法：set_1.union(set_2)\n",
    ">>> 2. 使用逻辑运算符：set_1 | set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d0138831",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 1.使用并集的方法：set_1.union(set_2)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1.union(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "897beb7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n"
     ]
    }
   ],
   "source": [
    "# 2. 使用逻辑运算符：set_1 | set_2\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f30b1c73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的时间 :  1638714360.21734\n",
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "交集结果 :  {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "结束的时间 :  1638714360.21734\n",
      "运行的时间 :  0.0\n"
     ]
    }
   ],
   "source": [
    "# 计算运行效率 = 运行后的时间节点 - 运行前的时间节点  : 思考并对比列表运算的执行效率 \n",
    "import time\n",
    "start_time = time.time()\n",
    "print(\"开始的时间 : \",start_time)\n",
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"交集结果 : \",set_1 | set_2)\n",
    "stop_time = time.time()\n",
    "print(\"结束的时间 : \",stop_time)\n",
    "print(\"运行的时间 : \", stop_time - start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76a326b1",
   "metadata": {},
   "source": [
    ">> 3. 集合的差集\n",
    ">>> 1. 使用差集的方法：set_1.difference(set_2)\n",
    ">>> 2. 使用逻辑运算符：set_1 - set_2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d7b8298c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1 和 set_2 差集结果 :  {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1.difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1af6e00c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_2 和 set_1 差集结果 :  {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2.difference(set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "82b13e3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_1 和 set_2 差集结果 :  {2, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_1 和 set_2 差集结果 : \",set_1 - set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "6f3afe73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "set_2 和 set_1 差集结果 :  {0, 8, 66}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"set_2 和 set_1 差集结果 : \",set_2 - set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "954aa5e8",
   "metadata": {},
   "source": [
    ">> 4.对称差集\n",
    ">>> 1. 用对称差集的方法：set_1.symmetric_difference\n",
    ">>> 2. 使用逻辑运算符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "d49e8cdf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function symmetric_difference:\n",
      "\n",
      "symmetric_difference(...) method of builtins.set instance\n",
      "    Return the symmetric difference of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in exactly one of the sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.symmetric_difference)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a36201d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果 :  {0, 66, 2, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",set_1.symmetric_difference(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "72428b20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 :  {1, 2, 3, 4, 5, 6, 7}\n",
      "set_2 :  {0, 1, 66, 3, 4, 7, 8}\n",
      "对称差集结果 :  {0, 2, 66, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1 : \",set_1)\n",
    "print(\"set_2 : \",set_2)\n",
    "print(\"对称差集结果 : \",(set_1 - set_2) | (set_2-set_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3e5116e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd79b4c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 集合实践：课本p129"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c0a1d072",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "unexpected EOF while parsing (<ipython-input-39-98b4094bc20b>, line 5)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-39-98b4094bc20b>\"\u001b[1;36m, line \u001b[1;32m5\u001b[0m\n\u001b[1;33m    print(vowel\u001b[0m\n\u001b[1;37m               ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m unexpected EOF while parsing\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:          \n",
    "    print(vowel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "6a8a52e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vowels :  {'o', 'e', 'u', 'a', 'i'}\n",
      "Provide a word to search for vowels:njskkviuv\n",
      "word :  {'n', 's', 'u', 'v', 'k', 'i', 'j'}\n",
      "{'u', 'i'}\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "print(\"vowels : \",vowels)\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "print(\"word : \",set(word))\n",
    "found = vowels.intersection(set(word))\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d6b81e2",
   "metadata": {},
   "source": [
    "# 课后作业：\n",
    "> 1. 课后作业;\n",
    "> 2. 课堂练习\n",
    "> 3. 课本第三章 课后项目练习\n",
    "> 4. 总结集合方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d652466",
   "metadata": {},
   "source": [
    "## 课本p129\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fcab96c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "提供一组单词或者句子来寻找元音:Do not go gentle into that good night.\n",
      "a\n",
      "o\n",
      "i\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"提供一组单词或者句子来寻找元音:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2e3ab39d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input a string（输入一个字符串）:\n",
      "Do not go gentle into that good night. 2021/12/18\n",
      "文字=30,空格=8,数字=8,其他字符个数=3\n"
     ]
    }
   ],
   "source": [
    "# 题1:输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数\n",
    "s=input('input a string（输入一个字符串）:\\n') # (\\n ==> 换行 )\n",
    "letters=0\n",
    "space=0\n",
    "digit=0\n",
    "others=0\n",
    "for c in s:\n",
    "    if c.isalpha():  # 检测字符是否包含字母 是返回True，否返回False\n",
    "        letters += 1\n",
    "    elif c.isspace(): # 检测字符是否包含空格 是返回True,否返回False\n",
    "        space += 1\n",
    "    elif c.isdigit(): # 检测字符是否包含数字 是否返回True,否返回Fasle\n",
    "        digit += 1\n",
    "    else:\n",
    "        others += 1\n",
    "print('文字=%d,空格=%d,数字=%d,其他字符个数=%d'%(letters,space,digit,others))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2b7e7c4",
   "metadata": {},
   "source": [
    "## 集合的概念\n",
    "* 集合（set）是一个**无序的不重复元素序列**。\n",
    "* 可以使用**大括号 { } 或者 set() 函数创建集合**，<font size=\"3\" color=\"red\" >注意：创建一个空集合必须用 set() 而不是 { }，因为 { } 是用来创建一个空字典。</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1147ae2d",
   "metadata": {},
   "source": [
    "## 集合方法："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fd7ad1e",
   "metadata": {},
   "source": [
    "\n",
    "| 方法 | 描述 |\n",
    "| :-----| :----- |\n",
    "| add() | 向集合添加元素。 |\n",
    "| clear() | 删除集合中的所有元素。 |\n",
    "| copy()  | **返回**集合的副本。 |\n",
    "| difference()  | 返回包含两个或更多集合之间差异的集合。 |\n",
    "| difference_update()  | 删除此集合中也包含在另一个指定集合中的项目。 |\n",
    "| discard()  | **删除指定**项目。 |\n",
    "| intersection()  | 返回为两个其他集合的交集的集合。 |\n",
    "| intersection_update() | 删除此集合中不存在于其他指定集合中的项目。 |\n",
    "| isdisjoint()  | 返回两个集合是否有交集。 |\n",
    "| issubset()  | 返回另一个集合是否包含此集合。 |\n",
    "| issuperset()  | 返回此集合是否包含另一个集合。 |\n",
    "| pop()  | \t从集合中删除一个元素。|\n",
    "| remove()  | 删除**指定**元素。 |\n",
    "| symmetric_difference()  | 返回具有两组集合的对称差集的集合。 |\n",
    "| symmetric_difference_update()  | **插入**此集合和另一个集合的对称差集。 |\n",
    "| union()  | 返回包含集合并集的集合。 |\n",
    "| update()  | 用此集合和其他集合的并集来更新集合。 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "423cc45e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7877e8f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd94fae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
